<template>
  <div class="detail">
    <!-- 顶部导航 -->
    <van-nav-bar
      title="详情页"
      left-text="返回"
      right-text="..."
      left-arrow
      @click-left="$router.go(-1)"
    />

      <!-- 店铺头部  -->
      <!-- shopinfo初次渲染的时候是空的 -->
      <!-- shopinfo.info && shopinfo.info.poi_back_pic_url 这里进行了判断让还没有数据时不渲染就不会报错 -->
      <div class="shop_header" :style="{background:'url('+(shopinfo.info && shopinfo.info.poi_back_pic_url) +')'}">
        <div class="left">
          <img :src="shopinfo.info && shopinfo.info.picUrl" alt="">
          <span>{{shopinfo.info && shopinfo.info.name}}</span>
        </div>
        <!-- 要实现点击后商品收藏，收藏后的商品再次点击后会取消收藏 -->
        <!-- 调用数据库中的异步方法，添加店铺到仓库数据的数组中 -->
      <!-- 取消收藏按钮 -->
      <div class="right" v-if="isCollect" @click="request_unfav(shopinfo.info)" style="color:red">❤</div>
      <!-- 收藏按钮 -->
      <div class="right" v-else @click="request_fav(shopinfo.info)">❤</div>
      </div>

    <!-- 店铺内的 商品分类和列表 -->
    <div class="box">
      <div class="left">
        <van-sidebar v-model="currentIndex">
          <van-sidebar-item  v-for="(item,index) in shopinfo.taglist" :key="index" :title="item.name" />
        </van-sidebar>
      </div>
      <div class="right">
        <div class="good" v-for="(item,index) in shopinfo.taglist && shopinfo.taglist[currentIndex].goodlist" :key="index">
          <div class="imgbox">
            <img :src="item.picture" alt="">
          </div>
          <div class="text">
            <div class="up">
              <div class="name">{{item.name}}</div>
              <div class="desc">{{item.month_saled_content}}</div>
            </div>
            <div class="down">
              <div class="price">¥{{item.min_price}}</div>
              <div class="opes">
                <span @click="request_shopcart_sub(item)" v-show="isInshopcart(item)">-</span>
                <span v-show="isInshopcart(item)">{{isInshopcartCount(item)}}</span>
                <span @click="request_shopcart_add(item)">+</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 购物车 -->
    <Shopcart ref="shopcart"></Shopcart>

    <!-- 提交栏 -->
    <div class="submitbar">
      <span><van-icon name="cart" size="20" color="darkturquoise" @click="$refs.shopcart.changeVisible()"></van-icon>
        总数：<span>{{totalCount}}</span>
        总价：<span>{{totalPrice}}</span>
      </span>
      <span >去结算</span>
    </div>
  </div>
</template>

<script>
//导入vuex中的工具函数(辅助函数) , 使用该工具函数 简化vuex在组件中的应用

import { mapState,mapMutations,mapGetters,mapActions } from 'vuex'
import Shopcart from '../components/Shopcart.vue'
import { shop_shopinfo } from '../utils/api'
// 提示框插件
import { Dialog } from 'vant';
export default {
  data(){
    return {
      currentIndex:0,
      shopinfo:{},
    }
  },
  components:{
    Shopcart,
  },
  computed:{
    // 计算属性中判断当前店铺是否已经收藏
    // 拿到当前店铺对象，还要同时拿到收藏列表，然后做判断，返回判断结果
    ...mapState(['collectlist','shopcartlist']),
    isCollect() {
      var index = this.$store.state.collectlist.findIndex((item) => {
        // return item.mtWmPoiId == this.shopinfo.mtWmPoiId;
        return item.mtWmPoiId == this.shopinfo.mtWmPoiId;
      });
      return index !== -1;
    },
    // 计算总数
    totalCount(){
      var sum = 0;
      this.shopcartlist.forEach((item)=>{
          sum += item.count;
      })
      return sum;
    },
    // 计算总价
    totalPrice(){
      var sum = 0;
      this.shopcartlist.forEach((item)=>{
          sum += item.count/1 * item.min_price/1;
      })
      return sum.toFixed(2);
    }
  },
  methods:{
    //mapMutations()这个工具函数 会将vuex中的同步方法 映射到组件的methods中.
    ...mapMutations([]),
    //mapActions()这个工具函数 会将vuex中的异步方法 映射到组件的methods中.
    ...mapActions(['request_fav','request_unfav','request_shopcart_add','request_shopcart_sub']),
    isInshopcart(item){//当前商品是否是需要判断的商品对象
      var index = this.shopcartlist.findIndex((it)=>{
        return it.id == item.id
      })
      return index !== -1
    },
    isInshopcartCount(item){//当前商品在购物车中的数量
      // item 是需要判断的商品对象
      var good = this.shopcartlist.find((it)=>{
        return it.id == item.id
      })
      if(good){//成立则说明，当前商品在购物车中，返回数量
        return good.count;
      }else{
        return 0;
      }

    }
  },
  // 点击店铺跳详情页后拿到该店铺的id
  mounted(){
    // console.log(this.$route.params.id);
    shop_shopinfo({id:this.$route.params.id}).then((res)=>{
      console.log(res.data);
      if(res.data.shopinfo){//店铺正常营业
        this.shopinfo = res.data.shopinfo;
      }else{ //店铺倒闭
        Dialog.alert({
          title: '提示',
          message: '该店铺已倒闭!'
        }).then(() => {
          this.$router.go(-1);
        });
      }
      // console.log(this.shopinfo);
    })
  }
}
</script>

<style lang='scss' scoped>
.shop_header{
  display: flex;
  justify-content: space-between;
  padding: 50px 10px;
  background-size: cover;
  background-repeat: no-repeat;
  color: white;
  .left{
    flex:1;
    display: flex;
    align-items: center;
    img{
      width: 30px;
      margin-right: 10px;
      border-radius: 50%;
    }
  }
  .right{
    width: 20px;
    text-align: right;
  }
}
.box{
  display: flex;
  .left{
    width: 80px;
  }
  .right{
    flex:1;
    .good{
      display: flex;
      margin: 10px;
      .imgbox{
        width: 80px;
        margin-right: 10px;
        img{
          width: 100%;
        }
      }
      .text{
        flex:1;
        font-size: 12px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .down{
          display: flex;
          justify-content: space-between;
          span{
            display: inline-block;
            width: 16px;
            height: 16px;
            background-color: lightskyblue;
            color: white;
            font-size: 12px;
            text-align: center;
            line-height: 16px;
            margin-right: 10px;
            &:nth-child(2){
              background-color: white;
            color: black;
            }
          }

        }
      }
    }
  }
}

.submitbar{
  background-color: darkslategrey;
  position: fixed;
  bottom:0;
  width: 100%;
  box-sizing: border-box;
  padding: 10px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: white;
  .summary{
    background-color: lime;
    color: white;
    display: inline-block;
    padding: 8px 15px;
    border-radius: 5px;
  }
}

</style>